{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Reverse Subarray To Maximize Array Value"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Hard"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #greedy #array #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #贪心 #数组 #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: maxValueAfterReverse"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #翻转子数组得到最大的数组值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数数组&nbsp;<code>nums</code> 。「数组值」定义为所有满足&nbsp;<code>0 &lt;= i &lt; nums.length-1</code>&nbsp;的&nbsp;<code>|nums[i]-nums[i+1]|</code>&nbsp;的和。</p>\n",
    "\n",
    "<p>你可以选择给定数组的任意子数组，并将该子数组翻转。但你只能执行这个操作&nbsp;<strong>一次</strong> 。</p>\n",
    "\n",
    "<p>请你找到可行的最大 <strong>数组值&nbsp;</strong>。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [2,3,1,5,4]\n",
    "<strong>输出：</strong>10\n",
    "<strong>解释：</strong>通过翻转子数组 [3,1,5] ，数组变成 [2,5,1,3,4] ，数组值为 10 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [2,4,9,24,2,1,10]\n",
    "<strong>输出：</strong>68\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 3*10^4</code></li>\n",
    "\t<li><code>-10^5 &lt;= nums[i] &lt;= 10^5</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [reverse-subarray-to-maximize-array-value](https://leetcode.cn/problems/reverse-subarray-to-maximize-array-value/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [reverse-subarray-to-maximize-array-value](https://leetcode.cn/problems/reverse-subarray-to-maximize-array-value/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[2,3,1,5,4]', '[2,4,9,24,2,1,10]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValueAfterReverse(self, nums: List[int]) -> int:\n",
    "        base = d = 0\n",
    "        mx, mn = -inf, inf\n",
    "        for a, b in pairwise(nums):\n",
    "            base += abs(a - b)\n",
    "            mx = max(mx, min(a, b))\n",
    "            mn = min(mn, max(a, b))\n",
    "            d = max(d, abs(nums[0] - b) - abs(a - b), # i = 0\n",
    "                        abs(nums[-1] - a) - abs(a - b)) # j = n - 1\n",
    "        return base + max(d, 2 * (mx - mn))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValueAfterReverse(self, nums: List[int]) -> int:\n",
    "        base = d = 0\n",
    "        mx, mn = -inf, inf\n",
    "        for a, b in pairwise(nums):\n",
    "            base += abs(a - b)\n",
    "            mx = max(mx, min(a, b))\n",
    "            mn = min(mn, max(a, b))\n",
    "            d = max(d, abs(nums[0] - b) - abs(a - b),  # i=0\n",
    "                       abs(nums[-1] - a) - abs(a - b))  # j=n-1\n",
    "        return base + max(d, 2 * (mx - mn))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValueAfterReverse(self, nums: List[int]) -> int:\n",
    "        base = d = 0\n",
    "        mx, mn = -inf, inf\n",
    "        for a, b in pairwise(nums):\n",
    "            base += abs(a - b)\n",
    "            mx = max(mx, min(a, b))\n",
    "            mn = min(mn, max(a, b))\n",
    "            d = max(d, abs(nums[0] - b) - abs(a - b),  # i=0\n",
    "                       abs(nums[-1] - a) - abs(a - b))  # j=n-1\n",
    "        return base + max(d, 2 * (mx - mn))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValueAfterReverse(self, nums: List[int]) -> int:\n",
    "        base = d = 0\n",
    "        mx, mn = -inf, inf\n",
    "        for a, b in pairwise(nums):\n",
    "            base += abs(a - b)\n",
    "            mx = max(mx, min(a, b))\n",
    "            mn = min(mn, max(a, b))\n",
    "            d = max(d, abs(nums[0] - b) - abs(a - b),  # i=0\n",
    "                       abs(nums[-1] - a) - abs(a - b))  # j=n-1\n",
    "        return base + max(d, 2 * (mx - mn))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValueAfterReverse(self, nums: List[int]) -> int:\n",
    "        base = d = 0\n",
    "        mx, mn = -inf, inf\n",
    "        for a, b in pairwise(nums):\n",
    "            base += abs(a - b)\n",
    "            mx = max(mx, min(a, b))\n",
    "            mn = min(mn, max(a, b))\n",
    "            d = max(d, abs(nums[0] - b) -abs(a - b), abs(nums[-1] - a) - abs(a - b))\n",
    "        return base + max(d, 2*(mx - mn))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValueAfterReverse(self, nums: List[int]) -> int:\n",
    "        md=s=0\n",
    "        mn,mx=-inf,inf\n",
    "        n=len(nums)\n",
    "        for i in range(1,n):\n",
    "            a,b=nums[i-1],nums[i]\n",
    "            d=abs(a-b)\n",
    "            s+=d\n",
    "            mn=max(mn,min(a,b))\n",
    "            mx=min(mx,max(a,b))\n",
    "            md=max(md,max(abs(b-nums[0])-d,abs(nums[n-1]-a)-d))\n",
    "        return s+max(2*mn-2*mx,md)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValueAfterReverse(self, nums: List[int]) -> int:\n",
    "        md=s=0\n",
    "        mn,mx=-inf,inf\n",
    "        n=len(nums)\n",
    "        for a,b in pairwise(nums):\n",
    "            d=abs(a-b)\n",
    "            s+=d\n",
    "            mn=max(mn,min(a,b))\n",
    "            mx=min(mx,max(a,b))\n",
    "            md=max(md,max(abs(b-nums[0])-d,abs(nums[n-1]-a)-d))\n",
    "        return s+max(2*mn-2*mx,md)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValueAfterReverse(self, nums: List[int]) -> int:\n",
    "        base = d = 0\n",
    "        mx, mn = -inf, inf\n",
    "        for a, b in pairwise(nums):\n",
    "            base += abs(a - b)\n",
    "            mx = max(mx, min(a, b))\n",
    "            mn = min(mn, max(a, b))\n",
    "            d = max(d, abs(nums[0] - b) - abs(a - b),  # i=0\n",
    "                       abs(nums[-1] - a) - abs(a - b))  # j=n-1\n",
    "        return base + max(d, 2 * (mx - mn))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValueAfterReverse(self, nums: List[int]) -> int:\n",
    "        value, n = 0, len(nums)\n",
    "        mx, mn = -inf, inf\n",
    "        for i in range(n - 1):\n",
    "            value += abs(nums[i] - nums[i + 1])\n",
    "        mx1 = 0\n",
    "        for i in range(1, n - 1):\n",
    "            mx1 = max(mx1, abs(nums[0] - nums[i + 1]) - abs(nums[i] - nums[i + 1]))\n",
    "            mx1 = max(mx1, abs(nums[-1] - nums[i - 1]) - abs(nums[i] - nums[i - 1]))\n",
    "        \n",
    "        for i in range(n - 1):\n",
    "            x, y = nums[i], nums[i + 1]\n",
    "            mx = max(mx, min(x, y))\n",
    "            mn = min(mn, max(x, y))\n",
    "        return value + max(mx1, 2 * (mx - mn))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValueAfterReverse(self, nums: List[int]) -> int:\n",
    "        value , n = 0,len(nums)\n",
    "        for i in range(n-1):\n",
    "            value += abs(nums[i] - nums[i+1])\n",
    "        mx1 = 0 \n",
    "        for i in range(1,n-1):\n",
    "            mx1 = max(mx1,abs(nums[0] - nums[i+1]) - abs(nums[i] - nums[i+1]))\n",
    "            mx1 = max(mx1,abs(nums[-1] - nums[i-1]) - abs(nums[i] - nums[i-1]))\n",
    "        \n",
    "        mx2,mn2 = -inf,inf \n",
    "        for i in range(n-1):\n",
    "            x,y = nums[i],nums[i+1]\n",
    "            mx2 = max(mx2,min(x,y))\n",
    "            mn2 = min(mn2,max(x,y))\n",
    "\n",
    "        return value + max(mx1,2 * (mx2-mn2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValueAfterReverse(self, nums: List[int]) -> int:\n",
    "        value, n,mx1 = 0, len(nums),0\n",
    "\n",
    "        for i in range(n - 1):\n",
    "            value += abs(nums[i] - nums[i + 1])\n",
    "        \n",
    "        for i in range(1, n - 1):\n",
    "            mx1 = max(mx1, abs(nums[0] - nums[i + 1]) - abs(nums[i] - nums[i + 1]),abs(nums[-1] - nums[i - 1]) - abs(nums[i] - nums[i - 1]))\n",
    "        mx, mn = -inf, inf\n",
    "        for i in range(n - 1):\n",
    "            x, y = nums[i], nums[i + 1]\n",
    "            mx = max(mx, min(x, y))\n",
    "            mn = min(mn, max(x, y))\n",
    "        return value + max(mx1, 2 * (mx - mn))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValueAfterReverse(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        if n == 1: \n",
    "            return 0\n",
    "        value_list = [abs(nums[i + 1] - nums[i]) for i in range(n - 1)]\n",
    "        value = sum(value_list)\n",
    "        m1 = min(nums[0], nums[1])\n",
    "        m2 = max(nums[0], nums[1])\n",
    "        ins = 0\n",
    "        for i in range(1, n - 1):\n",
    "            m1 = max(m1, min(nums[i], nums[i + 1]))\n",
    "            m2 = min(m2, max(nums[i], nums[i + 1]))\n",
    "            ins = max(ins, abs(nums[0] - nums[i + 1]) - value_list[i], \\\n",
    "                  abs(nums[i - 1] - nums[-1]) - value_list[i - 1])                      \n",
    "        return value + max(ins, (m1 - m2) << 1)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValueAfterReverse(self, nums: List[int]) -> int:\n",
    "        base = 0\n",
    "        mx = float('-inf')\n",
    "        mn = float('inf')\n",
    "        d = float('-inf')\n",
    "        n = len(nums) \n",
    "        for i in range(1,n):\n",
    "            a,b = nums[i], nums[i-1]\n",
    "            mn = min(mn, max(a,b))\n",
    "            mx = max(mx, min(a,b))\n",
    "            base += abs(a - b) \n",
    "            d = max(d, abs(nums[0] - nums[i]) - abs(a- b))\n",
    "            d = max(d, abs(nums[-1] - nums[i-1]) - abs(a - b)) \n",
    "        return base + max(d, 2*(mx-mn)) \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValueAfterReverse(self, nums: List[int]) -> int:\n",
    "        base = d = 0\n",
    "        mx, mn = -inf, inf\n",
    "        for a, b in pairwise(nums):\n",
    "            base += abs(a - b)\n",
    "            mx = max(mx, min(a, b))\n",
    "            mn = min(mn, max(a, b))\n",
    "            d = max(d, abs(nums[0] - b) - abs(a - b),  # i=0\n",
    "                       abs(nums[-1] - a) - abs(a - b))  # j=n-1\n",
    "        return base + max(d, 2 * (mx - mn))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValueAfterReverse(self, nums: List[int]) -> int:\n",
    "        value, n = 0, len(nums)\n",
    "        for i in range(n - 1):\n",
    "            value += abs(nums[i] - nums[i + 1])\n",
    "        mx1 = 0\n",
    "        for i in range(1, n - 1):\n",
    "            mx1 = max(mx1, abs(nums[0] - nums[i + 1]) - abs(nums[i] - nums[i + 1]))\n",
    "            mx1 = max(mx1, abs(nums[-1] - nums[i - 1]) - abs(nums[i] - nums[i - 1]))\n",
    "        mx2, mn2 = -inf, inf\n",
    "        for i in range(n - 1):\n",
    "            x, y = nums[i], nums[i + 1]\n",
    "            mx2 = max(mx2, min(x, y))\n",
    "            mn2 = min(mn2, max(x, y))\n",
    "        return value + max(mx1, 2 * (mx2 - mn2))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValueAfterReverse(self, nums: List[int]) -> int:\n",
    "        base = d = 0\n",
    "        mx, mn = -inf,inf\n",
    "        for a,b in pairwise(nums):\n",
    "            base += abs(a-b)\n",
    "            mx = max(mx,min(a,b))\n",
    "            mn = min(mn,max(a,b))\n",
    "            d = max(d,abs(nums[0]-b)-abs(a-b),abs(nums[-1]-a)-abs(a-b))\n",
    "        return base+max(d,2*(mx-mn))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValueAfterReverse(self, nums: List[int]) -> int:\n",
    "        base = d = 0\n",
    "        mx, mn = -inf, inf\n",
    "        for a, b in pairwise(nums):\n",
    "            base += abs(a - b)\n",
    "            mx = max(mx, min(a, b))\n",
    "            mn = min(mn, max(a, b))\n",
    "            d = max(d, abs(nums[0] - b) - abs(a - b),  # i=0\n",
    "                       abs(nums[-1] - a) - abs(a - b))  # j=n-1\n",
    "        return base + max(d, 2 * (mx - mn))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValueAfterReverse(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        if n == 1: \n",
    "            return 0\n",
    "        value_list = [abs(nums[i + 1] - nums[i]) for i in range(n - 1)]\n",
    "        value = sum(value_list)\n",
    "        m1 = min(nums[0], nums[1])\n",
    "        m2 = max(nums[0], nums[1])\n",
    "        ins = 0\n",
    "        for i in range(1, n - 1):\n",
    "            m1 = max(m1, min(nums[i], nums[i + 1]))\n",
    "            m2 = min(m2, max(nums[i], nums[i + 1]))\n",
    "            ins = max(ins, abs(nums[0] - nums[i + 1]) - value_list[i], \\\n",
    "                  abs(nums[i - 1] - nums[-1]) - value_list[i - 1])                      \n",
    "        return value + max(ins, (m1 - m2) << 1)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValueAfterReverse(self, nums: List[int]) -> int:\n",
    "        base,d = 0,0\n",
    "        mx,mn = -float('inf'), float('inf')\n",
    "        n = len(nums)\n",
    "        for i in range(n-1):\n",
    "            a,b = nums[i],nums[i+1]\n",
    "            base += abs(a-b)\n",
    "            mx = max(mx, min(a,b))\n",
    "            mn = min(mn, max(a,b))\n",
    "            d = max(d, abs(nums[0]-b) - abs(a-b), \\\n",
    "                        abs(nums[-1]-a) - abs(a-b))\n",
    "        return base + max(d, 2*(mx - mn))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValueAfterReverse(self, nums: List[int]) -> int:\n",
    "        value, n = 0, len(nums)\n",
    "        for i in range(n - 1):\n",
    "            value += abs(nums[i + 1] - nums[i])\n",
    "\n",
    "        mx1 = 0\n",
    "        for i in range(1, n-1):\n",
    "            mx1 = max(mx1, abs(nums[0] - nums[i + 1]) -\n",
    "                      abs(nums[i] - nums[i + 1]))\n",
    "            mx1 = max(mx1, abs(nums[-1] - nums[i - 1]) -\n",
    "                      abs(nums[i - 1] - nums[i]))\n",
    "        mx2, mn2 = -inf, inf\n",
    "        for i in range(n - 1):\n",
    "            x, y = nums[i], nums[i + 1]\n",
    "            mx2 = max(mx2, min(x, y))\n",
    "            mn2 = min(mn2, max(x, y))\n",
    "        return value + max(mx1 , 2 * (mx2 - mn2))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValueAfterReverse(self, nums: List[int]) -> int:\n",
    "        left_min = float('-inf')\n",
    "        right_max = float('inf')\n",
    "        ot = 0\n",
    "        sum_ = 0\n",
    "        for i in range(1,len(nums)):\n",
    "            a = nums[i-1]\n",
    "            b = nums[i]\n",
    "            diff = abs(a - b)\n",
    "            sum_ += diff\n",
    "            left_min = max(left_min,min(a,b))\n",
    "            right_max = min(right_max,max(a,b))\n",
    "            ot = max([ot,abs(b - nums[0]) - diff,abs(nums[-1] - a) - diff])\n",
    "        return max((left_min - right_max) * 2,ot) + sum_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValueAfterReverse(self, nums: List[int]) -> int:\n",
    "        ans = s = sum(abs(x - y) for x, y in pairwise(nums))\n",
    "        for x, y in pairwise(nums):\n",
    "            ans = max(ans, s + abs(nums[0] - y) - abs(x - y))\n",
    "            ans = max(ans, s + abs(nums[-1] - x) - abs(x - y))\n",
    "        for k1, k2 in pairwise((1, -1, -1, 1, 1)):\n",
    "            mx, mi = -inf, inf\n",
    "            for x, y in pairwise(nums):\n",
    "                a = k1 * x + k2 * y\n",
    "                b = abs(x - y)\n",
    "                mx = max(mx, a - b)\n",
    "                mi = min(mi, a + b)\n",
    "            ans = max(ans, s + max(mx - mi, 0))\n",
    "        return ans\n",
    "\n",
    "# class Solution:\n",
    "#     def maxValueAfterReverse(self, nums: List[int]) -> int:\n",
    "#     def func1(self,i,j,lst):\n",
    "#         sum2=0\n",
    "#         if i>0:\n",
    "#             org=abs(lst[i]-lst[i-1])\n",
    "#             new1=abs(lst[j]-lst[i-1])\n",
    "#             sum2=new1-org\n",
    "#         if j<len(lst)-1:\n",
    "#             org2=abs(lst[j]-lst[j+1])\n",
    "#             new2=abs(lst[i]-lst[j+1])\n",
    "#             sum2+=new2-org2\n",
    "#         return sum2\n",
    "#     def main(self,nums):\n",
    "#         sum,n=0,len(nums)\n",
    "#         for i in range(n-1):\n",
    "#             sum+=abs(nums[i]-nums[i+1])\n",
    "#         nums_distance=0\n",
    "#         for i in range(1,n-1):\n",
    "#             for j in range(2,n):\n",
    "#                 nums_distance = max(sum,self.func1(self,i,j,nums))\n",
    "#         return sum+nums_distance\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValueAfterReverse(self, nums: List[int]) -> int:\n",
    "        value, n = 0, len(nums)\n",
    "        for i in range(n - 1):\n",
    "            value += abs(nums[i] - nums[i + 1])\n",
    "        mx1 = 0\n",
    "        for i in range(1, n - 1):\n",
    "            mx1 = max(mx1, abs(nums[0] - nums[i + 1]) - abs(nums[i] - nums[i + 1]))\n",
    "            mx1 = max(mx1, abs(nums[-1] - nums[i - 1]) - abs(nums[i] - nums[i - 1]))\n",
    "        mx2, mn2 = -inf, inf\n",
    "        for i in range(n - 1):\n",
    "            x, y = nums[i], nums[i + 1]\n",
    "            mx2 = max(mx2, min(x, y))\n",
    "            mn2 = min(mn2, max(x, y))\n",
    "        return value + max(mx1, 2 * (mx2 - mn2))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValueAfterReverse(self, nums: List[int]) -> int:\n",
    "        value,n=0,len(nums)\n",
    "        for i in range(n-1):\n",
    "            value+=abs(nums[i]-nums[i+1])\n",
    "        mx1=0\n",
    "        for i in range(1, n - 1):\n",
    "            mx1 = max(mx1, abs(nums[0] - nums[i + 1]) - abs(nums[i] - nums[i + 1]))\n",
    "            mx1 = max(mx1, abs(nums[-1] - nums[i - 1]) - abs(nums[i] - nums[i - 1]))\n",
    "        mx2, mn2 = -inf, inf\n",
    "        for i in range(n - 1):\n",
    "            x, y = nums[i], nums[i + 1]\n",
    "            mx2 = max(mx2, min(x, y))\n",
    "            mn2 = min(mn2, max(x, y))\n",
    "        return value + max(mx1, 2 * (mx2 - mn2))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValueAfterReverse(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        sumn = maxn = 0\n",
    "        t = 100000\n",
    "        p = -100000\n",
    "        for i in range(n-1):\n",
    "            sumn += abs(nums[i+1] - nums[i])\n",
    "            maxn = max(maxn, abs(nums[n-1] - nums[i]) - abs(nums[i+1] - nums[i]))\n",
    "            maxn = max(maxn, abs(nums[i+1] - nums[0]) - abs(nums[i+1] - nums[i]))\n",
    "            u, v = max(nums[i], nums[i+1]), min(nums[i], nums[i+1])\n",
    "            if u < p:\n",
    "                maxn = max(maxn, 2*(p - u))\n",
    "            if v > t:\n",
    "                maxn = max(maxn, 2*(v - t))\n",
    "            t, p = min(t, u), max(p, v)\n",
    "        # print(sumn)\n",
    "        return (sumn + maxn)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValueAfterReverse(self, nums: List[int]) -> int:\n",
    "        res, l = 0, len(nums)\n",
    "        for i in range(l - 1):\n",
    "            res += abs(nums[i] - nums[i + 1])\n",
    "        val1 = 0\n",
    "        # 只翻转前半段 或者 只翻转后半段\n",
    "        for i in range(1, l - 1):\n",
    "            val1 = max(val1, abs(nums[0] - nums[i + 1]) - abs(nums[i] - nums[i + 1]), abs(nums[-1] - nums[i - 1]) - abs(nums[i - 1] - nums[i]))\n",
    "        # 翻转中间段\n",
    "        mx, mn = -10 ** 5, 10 ** 5\n",
    "        for i in range(l - 1):\n",
    "            mx = max(mx, min(nums[i], nums[i + 1]))\n",
    "            mn = min(mn, max(nums[i], nums[i + 1]))\n",
    "        return res + max(val1, 2 * (mx - mn))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValueAfterReverse(self, nums: List[int]) -> int:\n",
    "        value = 0\n",
    "        pos1, pos2 = -1, -1\n",
    "        #|x1-x2| + |y1-y2|  ||  |x1-y1| + |x2-y2|\n",
    "        #如果x1\n",
    "        neighborMax = [max(nums[i], nums[i + 1]) for i in range(len(nums) - 1)]\n",
    "        neighborMin = [min(nums[i], nums[i + 1]) for i in range(len(nums) - 1)]\n",
    "        #print(neighborMax, neighborMin)\n",
    "        offset = 2 * (max(neighborMin) - min(neighborMax))\n",
    "        offset = max(0, offset)\n",
    "        neighborValue = [abs(nums[i] - nums[i+1]) for i in range(len(nums) - 1)]\n",
    "        for i in range(1, len(nums) - 1):\n",
    "            #翻转0-i\n",
    "            offset2 = abs(nums[0] - nums[i + 1]) - abs(nums[i] - nums[i + 1])\n",
    "            #i-n-1\n",
    "            offset3 = abs(nums[-1] - nums[i-1]) - abs(nums[i] - nums[i-1])\n",
    "            offset = max(offset, offset2, offset3)\n",
    "        return sum(neighborValue) + offset\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxValueAfterReverse(self, nums: List[int]) -> int:\n",
    "        # 只有当数组左端点的最大值小于右数组右端点的最小值才能进行翻转\n",
    "        ans = d = 0\n",
    "        pair = [(nums[i], nums[i + 1]) for i in range(len(nums) - 1)]\n",
    "        minn, maxn = -float('inf'), float('inf')\n",
    "        for a, b in pair:\n",
    "            ans += abs(b - a)\n",
    "            minn = max(minn, min(a, b))\n",
    "            maxn = min(maxn, max(a, b))\n",
    "            d = max(d, abs(nums[0] - b) - abs(b - a), abs(nums[-1] - a) - abs(b - a))\n",
    "\n",
    "        return ans + max(d, 2 * (minn - maxn))\n",
    "\n",
    "         "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
